package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysMainComboMapper;
import com.ruoyi.system.mapper.SysOrdersMapper;
import com.ruoyi.system.mapper.SysOthersMapper;
import com.ruoyi.system.mapper.SysTotalDishesMapper;
import com.ruoyi.system.service.ISyOthersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class SysOthersServiceImpl implements ISyOthersService {
    private Long mealCount = 0L;
    private Long plainCount = 0L;
    private Long coldCount = 0L;
    private Long mainDrinkCount = 0L;
    private Long onlyCount = 0L;
    private Long douleCount = 0L;
    private Long threeCount = 0L;
    private Long familyCount = 0L;
    private Long dishesShoppingChange = 0L;
    private Long comboShoppingChange = 0L;
    private static  Integer flag=0;
    private static  Integer flagB=0;
    @Autowired
    private SysOthersMapper sysOthersMapper;
    @Autowired
    private SysOrdersMapper sysOrdersMapper;
    @Autowired
    private  SysTotalDishesMapper sysTotalDishesMapper;
    @Autowired
    private SysMainComboMapper sysMainComboMapper;
    private String dishesCodeAll = "";
    private String comboCodeAll = "";
    @Override
    public SysOtherDates getDatas() {
        SysOtherDates datas = sysOthersMapper.getDatas();
        List<SysOrders> orders = sysOrdersMapper.selectSysOrdersList(null);
       orders.stream().forEach(order ->{
           String dishesCode = order.getDishesCode();
           String comboCode = order.getComboCode();
           MPCM(dishesCode);
           ODTF(comboCode);
           dishesCodeAll += ("+"+dishesCode);
           comboCodeAll += ("+"+comboCode);
       });
        Map<Long, Long> dishesMap = calculateCounts(dishesCodeAll.substring(1));
        Map<Long, Long> comboMap = calculateCounts(comboCodeAll.substring(1));
        SysDishes dishes = new SysDishes();
        dishesMap.forEach((dishesId,shoppingChange) -> {
            System.out.println(dishesId);
            System.out.println(shoppingChange);
            dishes.setDishesId(dishesId);
            dishes.setShoppingChange(shoppingChange);
           sysOthersMapper.updateDishesShopping(dishes);
        });
        SysCombo sysCombo = new SysCombo();
        comboMap.forEach((comboDishesId,shoppingChange) -> {
            sysCombo.setComboDishesId(comboDishesId);
            sysCombo.setShoppingChange(shoppingChange);
            sysOthersMapper.updateComboShopping(sysCombo);
        });
        dishesShoppingChange = 0L;
        comboShoppingChange = 0L;
        datas.setMealCount(mealCount);
        datas.setPlainCount(plainCount);
        datas.setColdCount(coldCount);
        datas.setMainDrinkCount(mainDrinkCount);
        datas.setOnlyCount(onlyCount);
        datas.setDoubleCount(douleCount);
        datas.setThreeCount(threeCount);
        datas.setFamilyCount(familyCount);
        List<SysDishes> sysDishes = sysOthersMapper.selectDishesTop();
        List<SysCombo> sysCombos = sysOthersMapper.selectComboTop();
        Map<String, Long> dishesAndComboTop = new LinkedHashMap<>();
        dishesAndComboTop.put(sysDishes.get(0).getDishesName(),sysDishes.get(0).getShoppingChange());
        dishesAndComboTop.put(sysDishes.get(1).getDishesName(),sysDishes.get(1).getShoppingChange());
        dishesAndComboTop.put(sysDishes.get(2).getDishesName(),sysDishes.get(2).getShoppingChange());
        dishesAndComboTop.put(sysCombos.get(0).getComboName(),sysCombos.get(0).getShoppingChange());
        dishesAndComboTop.put(sysCombos.get(1).getComboName(),sysCombos.get(1).getShoppingChange());
        dishesAndComboTop.put(sysCombos.get(2).getComboName(),sysCombos.get(2).getShoppingChange());
        datas.setDishesAndComboTop(dishesAndComboTop);
        mealCount = 0L;
        plainCount = 0L;
        coldCount = 0L;
        mainDrinkCount = 0L;
        onlyCount = 0L;
        douleCount = 0L;
        threeCount = 0L;
        familyCount = 0L;
        dishesCodeAll = "";
        comboCodeAll = "";
        return datas;
    }

    @Override
    public SysOtherDates getDatasToday() {
        return sysOthersMapper.getDatasToday();
    }

    @Override
    public SysOtherTable getTableDatas() {
        SysOtherTable sysOtherTable = new SysOtherTable();
        Double[] sevenBM = new Double[7];
        Double[] sevenM = new Double[7];
        Long[] sevenBO = new Long[7];
        Long[] sevenO = new Long[7];
        List<SysOtherDates> datasSev = sysOthersMapper.getDatasSev();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = now.format(formatter);
        datasSev.stream().forEach(o ->{
            if (o.getOrdersCreatTime().equals(date)){
                sevenM[6]=o.getTotalMoney();
                sevenO[6]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-1).format(formatter))) {
                sevenM[5]=o.getTotalMoney();
                sevenO[5]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-2).format(formatter))) {
                sevenM[4]=o.getTotalMoney();
                sevenO[4]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-3).format(formatter))) {
                sevenM[3]=o.getTotalMoney();
                sevenO[3]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-4).format(formatter))) {
                sevenM[2]=o.getTotalMoney();
                sevenO[2]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-5).format(formatter))) {
                sevenM[1]=o.getTotalMoney();
                sevenO[1]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-6).format(formatter))) {
                sevenM[0]=o.getTotalMoney();
                sevenO[0]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-7).format(formatter))) {
                sevenBM[6]=o.getTotalMoney();
                sevenBO[6]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-8).format(formatter))) {
                sevenBM[5]=o.getTotalMoney();
                sevenBO[5]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-9).format(formatter))) {
                sevenBM[4]=o.getTotalMoney();
                sevenBO[4]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-10).format(formatter))) {
                sevenBM[3]=o.getTotalMoney();
                sevenBO[3]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-11).format(formatter))) {
                sevenBM[2]=o.getTotalMoney();
                sevenBO[2]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-12).format(formatter))) {
                sevenBM[1]=o.getTotalMoney();
                sevenBO[1]=o.getOrdersCount();
            } else if (o.getOrdersCreatTime().equals(now.plusDays(-13).format(formatter))) {
                sevenBM[0]=o.getTotalMoney();
                sevenBO[0]=o.getOrdersCount();
            }
        });
        System.out.println(sevenO[6]);
        for (int i = 0; i < 7; i++) {
            if (sevenBM[i]==null){
                sevenBM[i]=0.0;
            }
            if (sevenM[i]==null){
                sevenM[i]=0.0;
            }
            if (sevenBO[i]==null){
                sevenBO[i]=0L;
            }
            if (sevenO[i]==null){
                sevenO[i]=0L;
            }
        }
        sysOtherTable.setSevenBOrders(sevenBO);
        sysOtherTable.setSevenBMoney(sevenBM);
        sysOtherTable.setSevenOrders(sevenO);
        sysOtherTable.setSevenMoney(sevenM);
        return sysOtherTable;
    }
    private void MPCM(String dishesCode){
        sysOthersMapper.resetShoppingDishes();
        for (String dishesC : dishesCode.split("\\+")) {

            String[] split = dishesC.split("\\*");
            String dishesId = split[0];
            String shoppingChange = split[1];
            System.out.println(dishesId);
            SysDishes dishes = sysTotalDishesMapper.selectSysDishesByDishesId(Long.valueOf(dishesId));
            if (dishes.getMainType().equals("1")){
                if (dishes.getOtherType().equals("0")){
                    mealCount+=Long.valueOf(shoppingChange);
                } else if (dishes.getOtherType().equals("1")){
                    plainCount+=Long.valueOf(shoppingChange);
                }
                coldCount+=Long.valueOf(shoppingChange);
            } else if (dishes.getOtherType().equals("0")){
                mealCount+=Long.valueOf(shoppingChange);
            } else if (dishes.getOtherType().equals("1")){
                plainCount+=Long.valueOf(shoppingChange);
            } else if (dishes.getOtherType().equals("2")||dishes.getOtherType().equals("3")) {
                mainDrinkCount+=Long.valueOf(shoppingChange);
            }
        }
    }
    private  void ODTF(String comboCode){
        for (String comboC : comboCode.split("\\+")) {

            String[] split = comboC.split("\\*");
            String comboId = split[0];
            String shoppingChange = split[1];
            SysCombo combo = sysMainComboMapper.selectSysComboByComboDishesId(Long.valueOf(comboId));
            if (combo.getComboId().equals("1")){
                onlyCount+=Long.valueOf(shoppingChange);
            } else if (combo.getComboId().equals("2")){
                douleCount+=Long.valueOf(shoppingChange);
            } else if (combo.getComboId().equals("3")){
                threeCount+=Long.valueOf(shoppingChange);
            } else if (combo.getComboId().equals("4")) {
                familyCount+=Long.valueOf(shoppingChange);
            }
        }
    }
    public static Map<Long, Long> calculateCounts(String input) {
        // Step 1: Split the input string by '+'
        String[] parts = input.split("\\+");

        // Step 2: Initialize a map to store the sum of quantities for each number
        Map<Long, Long> countMap = new HashMap<>();

        // Step 3: Parse each part and update the map
        for (String part : parts) {
            String[] pair = part.split("\\*");
            Long number = Long.valueOf(pair[0].trim());
            Long quantity = Long.valueOf(pair[1].trim());

            // Map集合不会重复，因此不需要担心key的问题，而value： getOrDefault用于根据key返回对应的value，如果不存在返回0，加上quantity存入每个 菜品或者套餐对应的销量
            countMap.put(number, countMap.getOrDefault(number, 0L) + quantity);
        }

        return countMap;
    }
}
